/*
 * Copyright (C) 2011 Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * pn_Checking.java
 *
 * Created on 12/07/2011, 16:10:54
 */
package kuasar.plugin.deployer.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
import kuasar.plugin.Intercom.GUI;
import kuasar.plugin.deployer.gui.actions.th_checkVMs;
import kuasar.plugin.deployer.gui.classmod.I_Panels;
import kuasar.plugin.utils.pn_Info;
import org.jdom.Element;

/**
 *
 * @author Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 */
public class pn_Checker extends kuasar.plugin.classMod.AbstractPanel implements I_Panels{
    public Element target;
    public Element project;
    private JPanel previous;
    private int currentFrame = 0;
    private Timer imageConnection = new Timer (50, new ActionListener ()
        {
        @Override
            public void actionPerformed(ActionEvent e)
            {
                currentFrame++;
                javax.swing.ImageIcon icon;
                try{
                    icon = new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/deployer/icons/animation/connection/connection" + currentFrame + ".png"));
                }catch(NullPointerException ex){
                    currentFrame=1;
                    icon =  new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/deployer/icons/animation/connection/connection" + currentFrame + ".png"));
                }
                lbl_Image.setIcon(icon);
             }
        }); 
    /** Creates new form pn_Checking */
    public pn_Checker( Element project,Element target, JPanel previous) {
        this.target = target;
        this.project = project;
        this.previous=previous;
        initComponents();
        imageConnection.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbl_title = new javax.swing.JLabel();
        pn_Image = new javax.swing.JPanel();
        lbl_Info = new javax.swing.JLabel();
        lbl_left = new javax.swing.JLabel();
        lbl_right = new javax.swing.JLabel();
        lbl_Image = new javax.swing.JLabel();
        pn_Sub = new javax.swing.JPanel();
        btn_Next = new javax.swing.JButton();
        btn_Back = new javax.swing.JButton();

        setOpaque(false);

        lbl_title.setFont(new java.awt.Font("Dialog", 1, 28)); // NOI18N
        lbl_title.setForeground(new java.awt.Color(204, 204, 204));
        lbl_title.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/deployer/icons/checking.png"))); // NOI18N
        lbl_title.setText("Checking");

        pn_Image.setOpaque(false);

        lbl_Info.setForeground(new java.awt.Color(204, 204, 204));
        lbl_Info.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        lbl_Image.setForeground(new java.awt.Color(204, 204, 204));
        lbl_Image.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbl_Image.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/deployer/icons/animation/connection/connection39.png"))); // NOI18N
        lbl_Image.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        lbl_Image.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        javax.swing.GroupLayout pn_ImageLayout = new javax.swing.GroupLayout(pn_Image);
        pn_Image.setLayout(pn_ImageLayout);
        pn_ImageLayout.setHorizontalGroup(
            pn_ImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pn_ImageLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pn_ImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbl_Info, javax.swing.GroupLayout.DEFAULT_SIZE, 509, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pn_ImageLayout.createSequentialGroup()
                        .addComponent(lbl_left, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(lbl_Image, javax.swing.GroupLayout.PREFERRED_SIZE, 349, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lbl_right, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pn_ImageLayout.setVerticalGroup(
            pn_ImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pn_ImageLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pn_ImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbl_Image, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbl_right, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE)
                    .addComponent(lbl_left, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbl_Info, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pn_Sub.setOpaque(false);

        btn_Next.setBackground(new java.awt.Color(0, 0, 0));
        btn_Next.setForeground(new java.awt.Color(204, 204, 204));
        btn_Next.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/deployer/icons/next22.png"))); // NOI18N
        btn_Next.setText("Next");
        btn_Next.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_Next.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_NextActionPerformed(evt);
            }
        });

        btn_Back.setBackground(new java.awt.Color(0, 0, 0));
        btn_Back.setForeground(new java.awt.Color(204, 204, 204));
        btn_Back.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/deployer/icons/previous22.png"))); // NOI18N
        btn_Back.setText("Back");
        btn_Back.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btn_Back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_BackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pn_SubLayout = new javax.swing.GroupLayout(pn_Sub);
        pn_Sub.setLayout(pn_SubLayout);
        pn_SubLayout.setHorizontalGroup(
            pn_SubLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pn_SubLayout.createSequentialGroup()
                .addContainerGap(338, Short.MAX_VALUE)
                .addComponent(btn_Back)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btn_Next)
                .addContainerGap())
        );
        pn_SubLayout.setVerticalGroup(
            pn_SubLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pn_SubLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btn_Next)
                .addComponent(btn_Back))
        );

        btn_Next.setVisible(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pn_Sub, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pn_Image, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbl_title, javax.swing.GroupLayout.DEFAULT_SIZE, 533, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbl_title, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pn_Image, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(pn_Sub, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btn_BackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_BackActionPerformed
        goPrevious();
    }//GEN-LAST:event_btn_BackActionPerformed

private void btn_NextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_NextActionPerformed
        goNext();
}//GEN-LAST:event_btn_NextActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Back;
    private javax.swing.JButton btn_Next;
    private javax.swing.JLabel lbl_Image;
    private javax.swing.JLabel lbl_Info;
    private javax.swing.JLabel lbl_left;
    private javax.swing.JLabel lbl_right;
    private javax.swing.JLabel lbl_title;
    private javax.swing.JPanel pn_Image;
    private javax.swing.JPanel pn_Sub;
    // End of variables declaration//GEN-END:variables

    @Override
    public void goNext() {
        pn_Strategy strategy = new pn_Strategy(project, target, previous);
        GUI.loadPlugin(strategy);
    }

    @Override
    public void goPrevious() {
        JPanel parent = (JPanel) this.getParent();
        parent.removeAll();
        previous.setBounds(0, 0, parent.getWidth(), parent.getHeight());
        parent.add(previous);
        parent.updateUI();
    }

    public void start() {
        if(target == null || project ==null){
            pn_Info.Load((JPanel) this.getParent().getParent().getParent(), (JPanel) this.getParent().getParent(), "Error getting targets", "Checker has received a null object. Go back and try again.",pn_Info.ICON_ERROR);
            return;
        }
        th_checkVMs projects = new th_checkVMs(this);
        projects.start();
        
    }
    
    public void setInfoText(String text){
        lbl_Info.setText(text);
    }
    public void setVisibleNext(boolean state){
        btn_Next.setVisible(state);
    }
    public void showFinished(){
        lbl_title.setText("Checked!");
        imageConnection.stop();
        lbl_Image.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/deployer/icons/ok128.png")));
        lbl_Info.setText("All dependencies were solved.");
    }

}
